package com.sgb.zhaocai.view.demand.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.DemandIssueFragmentBinding;
import com.sgb.zhaocai.entity.DemandIssueListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.activity.DemandIssueActivity;
import com.sgb.zhaocai.view.demand.activity.DemandIssueListActivity;
import com.sgb.zhaocai.view.demand.adapter.DemandIssueAdapter;
import com.sgb.zhaocai.viewmodel.DemandIssueFragmentModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Time:2021/9/28
 * AUTHOR:F_Q
 * Description:
 */
public class DemandIssueFragment extends AppLazyFragment {
    private static String NO = "no";
    private static String SEARCHNAME = "SearchName";
    private String no = "";
    private DemandIssueFragmentModel mModel;
    private DemandIssueFragmentBinding mBinding;
    private DemandIssueAdapter demandissueadapter;

    private boolean isLoadMore = false;
    private RequestBean requestBean = new RequestBean();
    private String SearchName = "";

    public static DemandIssueFragment newInstance(String no, String SearchName) {
        DemandIssueFragment fragment = new DemandIssueFragment();
        Bundle bundle = new Bundle();
        bundle.putString(NO, no);
        bundle.putString(SEARCHNAME, SearchName);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.demand_issue_fragment, container, false);
        no = (String) getArguments().get(NO);
        SearchName = (String) getArguments().get(SEARCHNAME);
        mModel = new ViewModelProvider(this).get(DemandIssueFragmentModel.class);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        if (demandissueadapter == null) {
            demandissueadapter = new DemandIssueAdapter(getActivity(), null);
        }
        demandissueadapter.setOnItemStateOperate(new DemandIssueAdapter.OnItemStateOperate() {
            @Override
            public void Publicity(DemandIssueListEntity entity) {
                mModel.getIsPublicity(DemandIssueFragment.this, entity.getAnnounce(), entity.getId());
            }

            @Override
            public void CancelPublicity(DemandIssueListEntity entity) {
                mModel.getIsPublicity(DemandIssueFragment.this, entity.getAnnounce(), entity.getId());
            }

            @Override
            public void Edit(DemandIssueListEntity entity) {
                if (entity.getType().equals("4")) {
                    MToast.showToast(getContext(), "移动端暂不支持编辑分包计划需求，请前往电脑端进行编辑");
                } else {
                    startActivityForResult(new Intent(getActivity(), DemandIssueActivity.class).putExtra("id", entity.getId()).putExtra("type", 2), DemandIssueListActivity.DEMANDISSUE);
                }
            }

            @Override
            public void Details(DemandIssueListEntity entity) {
                mModel.getWebH5(DemandIssueFragment.this, entity.getId());
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setAdapter(demandissueadapter);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                //上拉加载更多
                mModel.getBusinessPlanList(DemandIssueFragment.this, requestBean);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        initDataList();
    }

    private void initDataList() {
        isLoadMore = false;
        requestBean.page = 1;
        if (!TextUtils.isEmpty(SearchName)) {
            requestBean.setName(SearchName + "");
            requestBean.setProjectName(SearchName + "");
        }else {
            requestBean.setName("");
            requestBean.setProjectName("");
        }
        if (!TextUtils.isEmpty(no)) {
            requestBean.setType(no);
        }else {
            requestBean.setType("");
        }
        if (mModel != null) {
            mModel.getBusinessPlanList(this, requestBean);
        }
    }

    public void setBusinessPlanList(List<DemandIssueListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                demandissueadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                demandissueadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                demandissueadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void setIsPublicity() {
        initDataList();
    }


    //搜索实现
    public void initializePop(String Name) {
        SearchName = Name;
        initDataList();
    }
}
